#include "CText.h"

CCText::~CCText(void)
{
	if (this->pTextColor)
	{
		delete this->pTextColor;
	}
	if (this->pFont)
	{
		TTF_CloseFont(this->pFont);
	}
	this->reduceTextNum();
	if (this->tellTextNum() == 0	)
	{
		TTF_Quit();
	}
}

CCText::CCText( std::string text )
	:pTextColor(NULL),
	strText(text),
	pFont(NULL)
{
	if ( textNum == 0 )
		if ( TTF_Init() < 0 )
			throw ("TTF_Init() failed!");
	this->increaseTextNum();
}

int CCText::textNum = 0;

void CCText::setTextRGB( Uint8 r, Uint8 g, Uint8 b )
{
	if (!this->pTextColor)
	{
		this->pTextColor = new SDL_Color;
	}
	this->pTextColor->r = r;
	this->pTextColor->g = g;
	this->pTextColor->b = b;
}

void CCText::setTextFont( std::string fontName, int fontSize /*= 80*/ )
{
	if (this->pFont)
	{
		TTF_CloseFont(this->pFont);
	}
	this->pFont = TTF_OpenFont(fontName.c_str(), fontSize);
	if ( pFont == 0 )
		throw "TTF_OpenFont() failed!";
}

SDL_Surface * CCText::drawable(){
	if (!this->pSurface)
	{
		this->pSurface = TTF_RenderText_Solid(this->pFont, this->strText.c_str(), *this->pTextColor);
		if ( this->pSurface == 0 )
			throw ("TTF_RenderText_solid() failed!");
	}
	
	return this->pSurface;
}

int CCText::tellTextNum() const
{
	return CCText::textNum;
}

void CCText::reduceTextNum()
{
	CCText::textNum--;
}

void CCText::increaseTextNum()
{
	CCText::textNum++;
}


